package com.zzl.gulimall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import com.zzl.gulimall.product.entity.BrandEntity;
import com.zzl.gulimall.product.service.BrandService;
import com.zzl.common.utils.PageUtils;
import com.zzl.common.utils.R;

import javax.validation.Valid;


/**
 *
 * @author zzl
 * @email zhuangzhongliang@163.com
 * @date 2021-03-10 14:06:58
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;



    /**
     * 修改
     */
    @RequestMapping("/update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(Long brandId,Integer showStatus){
        BrandEntity brandEntity = new BrandEntity();
        brandEntity.setBrandId(brandId);
        brandEntity.setShowStatus(showStatus);
        brandService.updateById(brandEntity);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Valid @RequestBody BrandEntity brand){
//        if (bindingResult.hasErrors()){
//            Map<String ,String > map = new HashMap<>();
//            bindingResult.getFieldErrors().forEach( item ->{
//                String name = item.getField();
//                String errMsg = item.getDefaultMessage();
//                map.put(name,errMsg);
//            });
//            return R.error(400, "提交的数据不合法").put("data",map);
//        }
		brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Valid @RequestBody BrandEntity brand){
		brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
